/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package trabalho3;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author mateus
 */
public class Controller {

    private View view;
    private JPAPaginationTableModel model;
    private OportunidadeJpaController controller;

    public Controller(View view, JPAPaginationTableModel model) {
        this.view = view;
        this.model = model;
        EntityManagerFactory managerFactory = Persistence.createEntityManagerFactory("Trabalho3PU");
        this.controller = new OportunidadeJpaController(managerFactory);
    }

    public void buttonNew() {
        String a = model.getTipo();
        Oportunidade oportunidade = new Estagio();
        if (a.equals("Bolsa")) {
            oportunidade = new Bolsa();
        }
        if (a.equals("Emprego")) {
            oportunidade = new Emprego();
        }
        controller.create(oportunidade);
        model.listaAtualiza();
        Tablechange();
    }

    public void buttonRadioTodos() {
        model.setTipo("Oportunidade");
        view.setButtonNew(false);
        view.setLabel7(null, false);
        view.setLabel8(false);
        view.setLabel9(false);
        Tablechange();
    }

    public void buttonRadioBolsa() {
        model.setTipo("Bolsa");
        view.setButtonNew(true);
        view.setLabel7("Nome lattes", true);
        view.setLabel8(true);
        view.setLabel9(true);
        Tablechange();
    }

    public void buttonRadioEstagio() {
        model.setTipo("Estagio");
        view.setButtonNew(true);
        view.setLabel7("Orgao", true);
        view.setLabel8(false);
        view.setLabel9(false);
        Tablechange();
    }

    public void buttonRadioEmprego() {
        model.setTipo("Emprego");
        view.setButtonNew(true);
        view.setLabel7("Beneficios", true);
        view.setLabel8(false);
        view.setLabel9(false);
        Tablechange();
    }

    public void buttonDelete(int rowselected) {
        if (rowselected == -1)
            return;
        model.deleta(rowselected);
    }

    public void buttonSearch(String text) {
        model.listaPesquisa(text);
        model.setTipo("Pequisa");
        view.setButtonNew(false);
        view.setLabel7(null, false);
        view.setLabel8(false);
        view.setLabel9(false);
        view.setRadioTodos(true);
        Tablechange();

    }

    public void buttonSave(int rowselected) {
        if (rowselected == -1)
             return;
        Oportunidade opor = model.getObjectrow(rowselected);
        opor.setTitulo(view.getTextfield(0));
        opor.setLocal(view.getTextfield(1));
        opor.setContato(view.getTextfield(2));
        opor.setDescricao(view.getTextfield(3));
        opor.setCarga_horaria(Integer.valueOf(view.getTextfield(4)));
        opor.setValor(Double.valueOf(view.getTextfield(5)));
        String str = model.getTipo();
        switch (str) {
            case "Estagio":
                Estagio est = (Estagio) opor;
                est.setOrgao(view.getTextfield(6));
                break;
            case "Emprego":
                Emprego emp = (Emprego) opor;
                emp.setBeneficios(view.getTextfield(6));
                break;
            case "Bolsa":
                Bolsa bols = (Bolsa) opor;
                bols.setNomelattes(view.getTextfield(6));
                bols.setEnderecolattes(view.getTextfield(7));
                bols.setDuracao(Integer.valueOf(view.getTextfield(8)));
                break;
        }
        controller.edit(opor);
        Tablechange();
    }

    private void Tablechange() {
        model.fireTableDataChanged();
        model.fireTableStructureChanged();
    }

    public void textfields(int row) {
        int i;
        String tipo = model.getTipo();
        for (i = 0; i < model.getColumnCount(); i++) {
            view.setTextFields(model.getValueAt(row, i).toString(), i);

        }

    }

}
